﻿using Microsoft.AspNetCore.Mvc.Filters;

namespace B.S.GridManager.Api.Write.Filters
{
    /// <summary>
    /// 全局异常过滤器
    /// </summary>
    public class GlobalExceptionFilter : IExceptionFilter
    {
        private readonly ILogger<GlobalExceptionFilter> logger;

        /// <summary>
        /// 构造函数,注入日志组件
        /// </summary>
        /// <param name="logger"></param>
        public GlobalExceptionFilter(ILogger<GlobalExceptionFilter> logger)
        {
            this.logger = logger;
        }

        public void OnException(ExceptionContext context)
        {
            logger.LogError("捕获到全局异常:"+context.Exception.Message);//使用日志组件记录错误信息
            context.ExceptionHandled = true;    //把错误信息标记为已处理
        }
    }
}
